package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.MemberGroup;
import com.yaowk.api.mall.clothing.service.IMemberGroupService;
import com.yaowk.api.mall.clothing.web.MemberGroupModel;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.dao.MemberGroupDao;
import com.yaowk.service.mall.clothing.entity.CmMemberGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class MemberGroupServiceImpl extends BaseServiceImpl<MemberGroup, CmMemberGroup, Integer> implements IMemberGroupService {

    @Autowired
    private MemberGroupDao dao;

    @Override
    public PageResp<MemberGroupModel> findAllOfWeb(PageReq pageReq) {
        // 查询列表
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize());
        Page<CmMemberGroup> page = getDao().findAll(pageRequest);

        // 构造返回
        PageResp<MemberGroupModel> pageResp = new PageResp<>();
        pageResp.setPageNumber(page.getNumber() + 1);
        pageResp.setPageSize(page.getSize());
        pageResp.setTotalPage(page.getTotalPages());
        pageResp.setTotalRow(page.getTotalElements());
        pageResp.setList(new ArrayList<>());

        List<MemberGroupModel> memberGroupModels = pageResp.getList();

        for (CmMemberGroup cmMemberGroup : page.getContent()) {
            MemberGroupModel memberGroupModel = new MemberGroupModel();
            memberGroupModel.setId(cmMemberGroup.getId());
            memberGroupModel.setName(cmMemberGroup.getName());
            memberGroupModel.setUpdatable(cmMemberGroup.getUpdatable());
            if (CollectionUtils.isEmpty(cmMemberGroup.getMembers())) {
                memberGroupModel.setMemberNum(0);
            } else {
                memberGroupModel.setMemberNum(cmMemberGroup.getMembers().size());
            }

            memberGroupModels.add(memberGroupModel);
        }

        return pageResp;
    }

    @Override
    public MemberGroup save(MemberGroup model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            CmMemberGroup memberGroup = dao.findOne(model.getId());
            if (isNotEmpty(model.getName())) {
                memberGroup.setName(model.getName());
            }
            model = entity2model(dao.save(memberGroup));
        }
        return model;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmMemberGroup model2Entity(MemberGroup model) {
        CmMemberGroup memberGroup = null;
        if (isNotEmpty(model)) {
            memberGroup = new CmMemberGroup();
            memberGroup.setId(model.getId());
            memberGroup.setName(model.getName());
            memberGroup.setUpdatable(model.getUpdatable());
        }
        return memberGroup;
    }

    @Override
    protected MemberGroup entity2model(CmMemberGroup entity) {
        MemberGroup memberGroup = null;
        if (isNotEmpty(entity)) {
            memberGroup = new MemberGroup();
            memberGroup.setId(entity.getId());
            memberGroup.setName(entity.getName());
            memberGroup.setUpdatable(entity.getUpdatable());
        }
        return memberGroup;
    }
}
